package entity;

import com.hzheima.util.ImageUtil;
import constant.BossConstant;
import constant.UiConstant;

import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

/**
 * @Author:zhengbowen
 * @Date: 2025/09/09
 * @Description:BOSS类
 */
public class Boss extends FlyingObject {
    // BOSS属性
    private int blood;
    private int currentBlood;
    private int xSpeed = 2;
    private int ySpeed = 1;
    private boolean movingRight = true; // 移动方向标志（true为向右）
    private Random random = new Random();
    private int shootCooldown = 0;     // 射击冷却计时器
    private static final int MAX_COOLDOWN = 120; // 最大冷却时间
    public static boolean bossLiveStatus;//Boss存活状态
    public static ArrayList<Bullet> bossBullets = new ArrayList<>(); // BOSS子弹集合

    public Boss() {
        bossLiveStatus=true;
        image = ImageUtil.getImg("ckx.jpg");
        width = image.getWidth() / 4;
        height = image.getHeight() / 4;
        x = UiConstant.APP_WIDTH / 2 - width / 2;
        y = 50;
        blood = BossConstant.BOSS_MAXBLOOD;
        currentBlood = blood;
    }

    public void move() {
        // 水平移动
        if (movingRight) {
            x += xSpeed;
            if (x > UiConstant.APP_WIDTH - width) {
                movingRight = false;
                y += 20; // 触右边界的向下移动
            }
        } else {
            x -= xSpeed;
            if (x < 0) {
                movingRight = true;
                y += 20; // 触左边界的向下移动
            }
        }

        // 限制BOSS在屏幕上方区域移动
        if (y < 30) y = 30;
        if (y > UiConstant.APP_HEIGHT / 3) y = UiConstant.APP_HEIGHT / 3;
    }

    public void shoot() {
        shootCooldown++;
        // 移动BOSS子弹并清理超出屏幕的子弹
        Iterator<Bullet> iterator = bossBullets.iterator();
        while (iterator.hasNext()) {
            Bullet bullet = iterator.next();
            bullet.move();
            // 反转移动方向（BOSS子弹向下移动）
            bullet.setY(bullet.getY() - 2 * bullet.getySpeed());

            // 移除超出屏幕的子弹
            if (bullet.getY() > UiConstant.APP_HEIGHT) {
                iterator.remove();
            }
        }
        if (shootCooldown >= MAX_COOLDOWN) {
            // 发射5发子弹
            for (int i = 0; i < 1; i++) {
                Bullet bullet = new Bullet(x + width / 2, y + height,1);

                bullet.setHeight(bullet.getHeight()*2);
                bullet.setWidth(bullet.getWidth()*2);
                // 设置子弹向下移动（反转默认的向上移动）
                bullet.setySpeed(-bullet.getySpeed());
                bossBullets.add(bullet);
                System.out.println(bossBullets.size());
            }
            shootCooldown = 0; // 重置冷却计时器
        }

    }
    @Override
    public void draw(Graphics g) {
        super.draw(g);
        // 绘制血条背景
        g.setColor(Color.red);
        g.fillRect(x, y - 20, width, 10);

        // 根据当前血量绘制血条
        int bloodWidth = (int) (((double) currentBlood / blood) * width);
        g.setColor(Color.green);
        g.fillRect(x, y - 20, bloodWidth, 10);

        // 绘制血量数字
        g.setColor(Color.white);
        g.setFont(new Font("楷体", Font.BOLD, 12));
        g.drawString(currentBlood + "/" + blood, x + width / 2 - 20, y - 12);
    }

    // Getter和Setter方法
    public int getCurrentBlood() {
        return currentBlood;
    }

    public void setCurrentBlood(int currentBlood) {
        this.currentBlood = currentBlood;
    }

    public int getBlood() {
        return blood;
    }

    public boolean hitBy(Bullet bullet) {
        boolean flag = bullet.y < this.y + this.height && bullet.y + bullet.height > this.y
                && bullet.x + bullet.width > this.x && bullet.x < this.x + this.width;
        if (flag) {
            currentBlood--;
        }
        return flag;
    }
}
